﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Qianke.Core.Common.NameMutex
{
    public class NameMutexCollection : INameMutex
    {
        private readonly IEnumerable<INameMutex> mutexes;

        public NameMutexCollection(IEnumerable<INameMutex> mutexes)
        {
            this.mutexes = mutexes;
        }

        public void Dispose()
        {
            _ = Task.Run(() => this.DisposeAsync());
        }

        public async ValueTask DisposeAsync()
        {
            foreach (var mutex in mutexes)
                await mutex.DisposeAsync();
        }

        public async Task EnterAsync()
        {
            foreach (var mutex in mutexes)
                await mutex.EnterAsync();
        }

        public async Task ExitAsync()
        {
            foreach (var mutex in mutexes)
                await mutex.ExitAsync();
        }

        public Task InitializeAsync(string mutexName)
        {
            return Task.CompletedTask;
        }

    
    }
}
